<?php

class DeviceController extends Common {

    private $deviceModel;

    public function __construct() {
        parent::__construct();
        $this->load->model('device');
        $this->deviceModel = $this->model('device_lib');
    }

    /**
     * 列表
     */
    public function listAction() {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        if(empty($userid)){
            echo "请重新登录";exit;
        }
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = ' 1=1 ';
        if($userid != 1){
            $where .= ' and pid = '.$userid;
        }
        $model = $this->model('device');
        $total = $model->count('device', 'id', $where);
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $model->page_limit($page, $pagesize)->order(array('id DESC'))->where($where)->select();
        $pagelist = $pagelist->total($total)->url(url('admin/device/list', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/device/device_list');  //这里是对视图文件的加载
    }

    /**
     * 列表
     */
    public function deviceLibListAction() {
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = '';
        $model = $this->model('device_lib');
        $total = $model->count('device_lib', 'id', $where);
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $model->page_limit($page, $pagesize)->order(array('id DESC'))->select();
        $pagelist = $pagelist->total($total)->url(url('admin/device/deviceLibList', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/device/device_lib_list');  //这里是对视图文件的加载
    }

    /**
     * 添加设备
     */
    public function addDeviceAction()
    {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        if ($this->post('submit')) {
            $data['sn'] = $_POST['sn'];     //设备sn号
            $data['2g'] = $_POST['2g'];     //2g卡号
            $data['sim'] = $_POST['sim'];   //sim卡号
            $data['status'] = 0;     //绑定状态 0：未绑定  1：绑定
            if(empty($data['sn']) || empty($data['2g']) || empty($data['sim'])){
                echo "设备sn号、2g卡号、sim卡号必填";exit;
            }
            $model = $this->model('device_lib');
            $isHave = $this->deviceModel->getDeviceByDeviceNum($data['2g']);  //查询设备库中是否存在该设备
            if($isHave->id){
                $this->adminMsg('改设备已存在', url('admin/device/addDevice'),3, 1, 0);
            }
            $data['create_time'] = $data['update_time'] = time();
            $insert_id = $model->insert($data);
            if(empty($insert_id)){
                $this->memberMsg('添加失败', url('admin/device/addDevice'),3, 1, 0);
            }
            $this->adminMsg('添加成功', url('admin/device/addDevice'), 3, 1, 1);
        }
        $this->view->display('/admin/device/add_device');  //这里是对视图文件的加载
    }

    /**
     * 分配设备
     */
    public function allotDeviceAction()
    {
        if ($this->post('submit')) {
            $data['agentId'] = intval($_POST['agentId']);     //代理商id
            $data['num'] = intval($_POST['num']);     //所需分配代理商的数量
            if(empty($data['agentId']) || empty($data['num'])){
                echo "代理商id、分配设备数量";exit;
            }
            $data['update_time'] = time();
            $data['userid'] = $this->session->get('user_id');   //从session中获取用户id
            $devCount = $this->deviceModel->getDeviceLibCount($data['userid']);    //获取设备库中未分配的设备数量
            if($devCount->num < $data['num']){
                echo "设备数量不足";exit;
            }
            $isUpdate = $this->deviceModel->alletDevice($data);     //为代理商分配设备号
            if($isUpdate == false){
                $this->memberMsg('分配失败', url('admin/device/allotDevice'),3, 1, 0);
            }
            $this->adminMsg('分配成功', url('admin/device/allotDevice'), 3, 1, 1);
        }
        $this->view->display('/admin/device/allot_device');  //这里是对视图文件的加载
    }

    /**
     * 用户资金流水记录
     */
    public function balanceInfoAction()
    {
        $uid = $this->get('uid')?:0;
        if(empty($uid)){echo '参数有误';}
        $model = $this->model('withdraw');
        $data = $model->getAll('uid=?',$uid,'*','id desc');  //用户资金流水记录
        $this->view->assign(array(
            'data' => $data,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/finance/ubalance_list');  //这里是对视图文件的加载
    }


    /**
     * 设备收入列表
     */
    public function deviceIncomeAction() {
        $deviceId = $this->get('device_id');   //设备id
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = "device_id = '$deviceId'";
        $model = $this->model('advertisement_push_record');
        $total = $model->count('advertisement_push_record', 'id', $where);
        $urlparam = array(
            'page' => '{page}',
            'device_id' => $deviceId,
        );
        $data = $model->page_limit($page, $pagesize)->order(array('id DESC'))->where($where)->select();
        $pagelist = $pagelist->total($total)->url(url('admin/device/deviceIncome', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/device/deviceIncome_list');  //这里是对视图文件的加载
    }

    public function sfAction(){
        $username=$_POST[username];   //获取视图文件传过来的表单的值  表单我用post传值
        // $model = $this->model('member');  //实例化model 这里的两行是用fine的原有的写的，但是我感觉这样不灵活我下面又用php的写了因为我感觉还是php写的顺手，大家自己感觉什么好就用什么吧
        // $data = $model->getAll('username=?', "$username"); //用的fine的getall函数
        $sql="select * from fn_member where username='$username' ";   //sql语句  这里是php的
        $row=mysql_query($sql);  //php执行sql
        $data=mysql_fetch_assoc($row);  //获取sql语句得到的值
        $this->view->assign('data',$data); //assign()对视图变量完成赋值
        $this->view->display('sf'); //加载视图文件
    }

}
